package com.itheima.im.socket.adapter;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

import com.itheima.im.socket.R;
import com.itheima.im.socket.activity.MyApp;
import com.itheima.im.socket.bean.QQBuddy;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

//BaseAdapter x4
//  |--ArrayAdapter x1
public class ContactAdapter extends ArrayAdapter<QQBuddy> {

	public ContactAdapter(Context context, List<QQBuddy> objects) {
		super(context, 0, objects);
	}

	// 1. static 2.ButterKnife.inject(this, view);
	static class ViewHolder {
		@InjectView(R.id.head)
		ImageView head;
		@InjectView(R.id.title)
		TextView title;
		@InjectView(R.id.desc)
		TextView desc;

		public ViewHolder(View view) {
			ButterKnife.inject(this, view);
		}

	}

	// 返回行视图，显示指定下标的数据
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// 数据
		QQBuddy info = getItem(position);
		ViewHolder holder = null;
		// 视图 1.inflate 内存
		if (convertView == null) {
			// 打气 inflate 将xml转换成对象
			// convertView=View.inflate(上下文, 视图文件, null);
			convertView = View.inflate(getContext(), R.layout.item_contact, null);
			holder = new ViewHolder(convertView);
			// 2.findViewById
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.title.setText(info.nick);
		if(MyApp.username.equals(info.account+""))
		{
			holder.title.setText("[我自己]");
		}
		holder.desc.setText(info.account+"@qq.com");
		return convertView;
	}

}
